import request from '@/utils/request'
import type {
  FavoriteItem,
  CreateFavoriteItemRequest,
  UpdateFavoriteItemRequest,
  FavoriteItemQuery
} from '../types'

// 导出统一的分类API
export { categoryApi } from './category'
export type { CategoryListItem, CategoryRequest, DragRequest } from './category'

// 收藏项API
export const favoriteItemApi = {
  // 查询收藏项列表（返回数组，不分页）
  async getList(params: FavoriteItemQuery): Promise<FavoriteItem[]> {
    const queryParams = new URLSearchParams()
    if (params.categoryId) queryParams.append('categoryId', params.categoryId)
    if (params.type) queryParams.append('type', params.type)
    if (params.keyword) queryParams.append('keyword', params.keyword)
    if (params.isFavorite !== undefined) queryParams.append('isFavorite', params.isFavorite.toString())

    const url = `/collect/list${queryParams.toString() ? '?' + queryParams.toString() : ''}`
    const response = await request.get<FavoriteItem[]>(url)
    return response.data
  },

  // 获取收藏项详情
  async getDetail(id: string): Promise<FavoriteItem> {
    const response = await request.get<FavoriteItem>(`/collect/${id}`)
    return response.data
  },

  // 创建收藏项
  async create(data: CreateFavoriteItemRequest): Promise<string> {
    const response = await request.post<string>('/collect/create', data)
    return response.data
  },

  // 更新收藏项
  async update(id: string, data: UpdateFavoriteItemRequest): Promise<void> {
    await request.put(`/collect/${id}`, data)
  },

  // 删除收藏项
  async delete(id: string): Promise<void> {
    await request.delete(`/collect/${id}`)
  },

  // 切换收藏状态
  async toggleFavorite(id: string): Promise<void> {
    await request.post(`/collect/${id}/toggle-favorite`)
  }
}